<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Folder_lib
 *
 * @author Administrator
 */
class Folder_lib {

  //put your code here
  protected $ci;

  public function __construct() {
    $this->ci = & get_instance();
  }
  
  public function get_as_tree($parent_id = '', $escapse_id = array()) {
    if (!empty($escapse_id)) {
      if (is_array($escapse_id)) {
        $this->ci->db->where_not_in('id', $escapse_id);
      } else {
        $this->ci->db->where('id !=', $escapse_id);
      }
    }
    if (!empty($parent_id)) {
      $root_folders = $this->ci->db->get_where(DB_TABLE_FOLDER, array('parent_id' => $parent_id))->result_array();
    } else {
      $root_folders = $this->ci->db->get_where(DB_TABLE_FOLDER, array('parent_id' => 0))->result_array();
    }
    foreach ($root_folders as $i => $folder) {
      $root_folders[$i]['children'] = $this->get_as_tree($folder['id'], $escapse_id);
    }
    return $root_folders;
  }

  public function get_as_tree_html($parent_id = '', $level = 0, $current_id = 0, $escapse_id = array()) {
    $html = '';
    if (!empty($escapse_id)) {
      if (is_array($escapse_id)) {
        $this->ci->db->where_not_in('id', $escapse_id);
      } else {
        $this->ci->db->where('id !=', $escapse_id);
      }
    }
    if (!empty($parent_id)) {
      $root_folders = $this->ci->db->get_where(DB_TABLE_FOLDER, array('parent_id' => $parent_id))->result_array();
    } else {
      $root_folders = $this->ci->db->get_where(DB_TABLE_FOLDER, array('parent_id' => 0))->result_array();
    }
    foreach ($root_folders as $i => $folder) {
      $temp = '';
      $html .= '<option '.($current_id == $folder['id'] ? 'selected="selected"' : '').' value="'.$folder['id'].'">';
      for ($j = 0; $j < $level; $j++) {
        $html .= '»';
      }
      $html .= ' '.$folder['name'] . '</option>';
      $html .= $this->get_as_tree_html($folder['id'], $level + 1, $current_id, $escapse_id);
    }
    return $html;
  }
  
  public function tree_folder($parent_id = 0, $current_dir = 0, $level = 0) {
    $html = '';
    if (!empty($escapse_id)) {
      if (is_array($escapse_id)) {
        $this->ci->db->where_not_in('id', $escapse_id);
      } else {
        $this->ci->db->where('id !=', $escapse_id);
      }
    }
    if (!empty($parent_id)) {
      $root_folders = $this->ci->db->get_where(DB_TABLE_FOLDER, array('parent_id' => $parent_id))->result_array();
    } else {
      $root_folders = $this->ci->db->get_where(DB_TABLE_FOLDER, array('parent_id' => 0))->result_array();
    }
    foreach ($root_folders as $i => $folder) {
      $temp = '';
      $html .= '<li class="'.(!empty($current_dir) && $current_dir == $folder['id'] ?'current' :'').'">';
      $html .= '<a class="folder-hash" title="'.$folder['name'].
              '" href="'.site_url($this->ci->router->fetch_class().'/index/'.$folder['id']).'">';
      for ($j = 0; $j < $level; $j++) {
        $html .= '»';
      }
      $html .= ' '.$folder['name'] . '</a></li>';
      $html .= $this->tree_folder($folder['id'], $current_dir, $level + 1);
    }
    return $html;
  }

}